package ne.analyzer.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.graphics.Image;

/**
 * 全局函数
 * 
 * @author sunny
 *
 */
public class Utils {
	public static String join(List<String> strs, String sep) {
		if (strs == null)
			return "";
		StringBuilder sb = new StringBuilder();
		for (String str : strs) {
			sb.append(str).append(sep);
		}
		String result = sb.toString();
		int len = sep.length();
		if (result.length() > len) {
			result = result.substring(0, result.length() - len);
		}
		return result;
	}

	public static String join(List<String> strs) {
		return join(strs, ", ");
	}

	public static Image getImageFromClassPath(String path) {
		InputStream imgStream = Utils.class.getResourceAsStream(path);
		Image img = new Image(null, imgStream);
		try {
			imgStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}

	/**
	 * 将字符串内容保存到文件
	 * 
	 * @param path
	 *            文件路径
	 * @param content
	 *            字符串内容
	 * @param encoding
	 *            字符串保存的编码格式
	 * @return
	 */
	public static boolean string2File(String path, String content, String encoding) {
		FileOutputStream fos;
		byte[] buffer;
		try {
			fos = new FileOutputStream(path);
			if (encoding == null)
				buffer = content.getBytes();
			else
				buffer = content.getBytes(encoding);
			fos.write(buffer);
			fos.flush();
			fos.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean string2File(String path, String content) {
		return string2File(path, content, null);
	}

	public static String file2String(String path, String encoding) {
		FileInputStream fis;
		byte[] buffer;
		try {
			fis = new FileInputStream(path);
			buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			return new String(buffer, encoding);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 读取指定的属性文件
	 * 
	 * @param path
	 *            属性文件路径
	 * @return
	 */
	public static Properties readProperties(String path) {
		Properties p = new Properties();
		InputStream is = null;
		try {
			if (path.startsWith("classes:")) {
				is = Utils.class.getResourceAsStream(path.substring(8));
			} else {
				is = new FileInputStream(path);
			}
			if (is != null) {
				p.load(is);
				is.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 将属性对象写入到属性文件
	 * 
	 * @param path
	 *            属性文件路径
	 * @param p
	 *            属性对象
	 * @return
	 */
	public static boolean writeProperties(String path, Properties p) {
		Enumeration<Object> keys = p.keys();
		StringBuilder sb = new StringBuilder();
		while (keys.hasMoreElements()) {
			String key = (String) keys.nextElement();
			sb.append(key);
			String value = p.getProperty(key);
			sb.append("=").append(value).append("\n");
		}
		return string2File(path, sb.toString());
	}

	/**
	 * 获取当前工具安装路径
	 * 
	 * @return
	 */
	public static String getInstallPath() {
		String path = Platform.getInstallLocation().getURL().toString();
		if (path.startsWith("file:"))
			path = path.substring(5);
		return path;
	}

	public static List<String> splitWith(String content, String sep) {
		List<String> result = new ArrayList<String>();
		if (content != null && !content.equals("")) {
			String[] rs = content.split(sep);
			for (String r : rs) {
				if (!r.trim().equals(""))
					result.add(r.trim());
			}
		}
		return result;
	}
}
